Chapter 23 ggplot2 tutorial

ggplot(gapminder, aes(x = gdpPercap, y = lifeExp)) +
  geom_point()

p <- ggplot(gapminder, aes(x = gdpPercap, y = lifeExp))
p + geom_point()

ggplot(gapminder, aes(log10(gdpPercap), y = lifeExp )) + 
  geom_point()

p + geom_point() +
  scale_x_log10() ## better way to log transfrom, consist with ggplot2 syntax

p <- p + scale_x_log10()

p + geom_point(aes(color = continent))

plot(gapminder, aes(x = gdpPercap, y = lifeExp, color = continent)) +
  geom_point() + scale_x_log10()

## NULL
p + geom_point(alpha = (1/3), size = 3)

p + geom_point() + geom_smooth()
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'

p + geom_point() + geom_smooth(lwd = 3, se = FALSE)
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'

p + geom_point() + geom_smooth(lwd = 3, se = FALSE, method = "lm")

p + aes(color = continent) + geom_point() + geom_smooth(lwd = 3, se = FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

p + geom_point(alpha = (1/3), size = 3) + 
  facet_wrap(~ continent)

p + geom_point(alpha = (1/3), size = 3) + 
  facet_wrap(~ continent) + 
  geom_smooth( lwd = 2, se = FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

ggplot(gapminder, aes(x = lifeExp, y = year, color = continent)) + geom_jitter(alpha = 1/3, size = 3) +
  geom_smooth(se = FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

ggplot(gapminder, aes(x = year, y = lifeExp, color = continent)) + geom_jitter(alpha = 1/3, sie = 3) + 
  facet_wrap(~ continent, scales = "free_x") +
  scale_color_manual(values = continent_colors)
## Warning: Ignoring unknown parameters: sie

ggplot(subset(gapminder, continent != "Oceania"),
       aes(x = year, y = lifeExp, group = country, color = country)) +
  geom_line(lwd = 1, show_guide = FALSE) + facet_wrap(~ continent) +
  scale_color_manual(values = country_colors) +
  theme_bw() + theme(strip.text = element_text(size = rel(1.1)))
## Warning: `show_guide` has been deprecated. Please use `show.legend`
## instead.

ggplot(gapminder, aes(x = year, y = lifeExp, color = continent)) + 
  geom_jitter(alpha = 1/3, size = 3) +
  geom_smooth(lwd = 2,se = FALSE) + 
  facet_wrap(~ continent, scales= "free_x") +
  scale_color_manual(values = continent_colors)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

ggplot(gapminder, aes(x = gdpPercap, y = lifeExp)) +
  scale_x_log10() + geom_bin2d()

ggplot(gapminder, aes(x = continent, y = lifeExp)) +
  geom_point()

gapminder %>% 
  ggplot(aes(x = fct_reorder(continent, lifeExp), y = lifeExp)) + 
  geom_jitter()

ggplot(gapminder, aes(x = fct_reorder(continent,lifeExp), y = lifeExp)) +
  geom_jitter(position = position_jitter(width = 0.1, height = 0), alpha = 1/4)

ggplot(gapminder, aes(x = fct_reorder(continent, lifeExp), y = lifeExp) ) +
  geom_boxplot()

ggplot(gapminder, aes(x = fct_reorder(continent, lifeExp), y = lifeExp)) +
  geom_jitter(position = position_jitter(width = 0.1, height = 0), alhpa = 1/4) +
  geom_boxplot( outlier.color = "hotpink")
## Warning: Ignoring unknown parameters: alhpa

ggplot(gapminder, aes(reorder(x = continent, lifeExp), y = lifeExp)) +
  geom_jitter(position = position_jitter(width = 0.1), alpha = 1/4) +
  stat_summary(fun.y = median, color = "red", geom = "point", size = 5)

ggplot(gapminder, aes(x = lifeExp)) +
  geom_histogram(binwidth = 1)

ggplot(gapminder, aes(x = lifeExp, fill = continent)) +
  geom_histogram() ##different continent overlope with each other, not a good plot
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(gapminder, aes(x = lifeExp, fill = continent)) +
  geom_histogram(position = "identity") ## Still, overlope :(
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(gapminder, aes(x = lifeExp, color = continent)) +
  geom_freqpoly()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(gapminder, aes(x = lifeExp)) + geom_density()

ggplot(gapminder, aes(x = lifeExp)) + geom_density(adjust = 1)

ggplot(gapminder, aes(x = lifeExp)) + geom_density(adjust = 0.2)

ggplot(gapminder, aes(x = lifeExp, color = continent)) +
  geom_density()

ggplot(gapminder, aes(x = lifeExp, fill = continent)) +
  geom_density(alpha = 0.2)

ggplot(subset(gapminder, continent!= "Oceania"), aes(x = lifeExp, fill = continent)) +
  geom_density(alpha = 0.2)

ggplot(subset(gapminder, continent!= "Oceania"), aes(x = lifeExp, fill = continent)) +
  geom_density(alpha = 0.2) + facet_wrap(~continent)

ggplot(subset(gapminder, continent != "Oceania"), aes(x = lifeExp, fill = continent)) + 
  geom_histogram() + facet_grid(continent~.)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(subset(gapminder, continent != "Oceania"), aes(x = lifeExp, fill = continent)) + 
  geom_histogram() + facet_grid(.~continent)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(gapminder, aes(x = year, y = lifeExp)) + geom_boxplot()
## Warning: Continuous x aesthetic -- did you forget aes(group=...)?

ggplot(gapminder, aes(x = year, y = lifeExp)) + geom_boxplot(aes(group = year))

ggplot(gapminder, aes(x = year, y = lifeExp)) +
  geom_violin(aes(group = year)) +
  geom_jitter(alpha = 1/4) +
  geom_smooth(se = FALSE, method = "lm")